#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "SDL/SDL.h"

int delay(lua_State *L)
{
    SDL_Delay(luaL_checkint(L,1));
    return 0;
}

int ticks(lua_State *L)
{
    lua_pushinteger(L, SDL_GetTicks());
    return 1;
}

luaL_Reg time_functions[] = {
    {"delay",delay},
    {"ticks",ticks},
    {NULL,NULL}
};

int luaopen_time(lua_State* L)
{
    if (!SDL_WasInit(SDL_INIT_TIMER))
        SDL_InitSubSystem(SDL_INIT_TIMER);

    lua_pushvalue(L, LUA_GLOBALSINDEX);
    luaL_register(L, NULL, time_functions);

    lua_pushboolean(L,1);
    return 1;
}
